﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.shop
{
    public partial class shoplevel_edite : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("013002002","对不起，您没有权限进行编辑");
                YXShop.Common.PromptInfo.Popedom("013002004", "对不起，您没有权限进行编辑");
                if (ChangeHope.WebPage.PageRequest.GetInt("id") > 0)
                {
                    BandInfo(ChangeHope.WebPage.PageRequest.GetInt("id"));
                }
                InitWebControl();
            }
        }

        #region 验证
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtName, "输入类别的名称", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtPrice, "输入该等级店铺的租金费用", "isint", "数字", "该项必须为数字类型");
            ChangeHope.WebPage.WebControl.Validate(this.txtExpendDays, "输入该店铺的有效期", "isint", "数字", "该项必须为数字类型");
            ChangeHope.WebPage.WebControl.Validate(this.txtShopSpace, "输入该店铺所拥有的空间大小", "isint", "数字", "该项必须为数字类型");
            ChangeHope.WebPage.WebControl.Validate(this.txtProCount, "输入该店铺所能上传的商品数量上限", "isint", "数字", "该项必须为数字类型");
            ChangeHope.WebPage.WebControl.Validate(this.txtContent, "输入该店铺的描述", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtSort, "请选择类别优先级，数字越大排序越靠后", "isint", "数字", "该项必须为数字类型");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #endregion

        /// <summary>
        /// 保存
        /// </summary>
        protected void Save()
        {
            YXShop.BLL.Shop.ShopLevel bll = new YXShop.BLL.Shop.ShopLevel();
            YXShop.Model.Shop.ShopLevel model = new YXShop.Model.Shop.ShopLevel();
            model.Name = this.txtName.Text.Trim();
            if (txtPrice.Text.Trim()!="")
            {
                if(!ChangeHope.Common.ValidateHelper.IsMoney(txtPrice.Text.Trim()))
                {
                    this.ltlMsg.Text = "操作失败，请输入正确价格。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                if(float.Parse(txtPrice.Text.Trim())<=0)
                {
                    this.ltlMsg.Text = "操作失败，价格小于或等于0。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            if (txtExpendDays.Text.Trim()!="")
            {
                if (!ChangeHope.Common.ValidateHelper.IsNumber(txtExpendDays.Text.Trim()))
                {
                    this.ltlMsg.Text = "操作失败，请输入正确天数。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                if(int.Parse(txtExpendDays.Text.Trim())<=0)
                {
                    this.ltlMsg.Text = "操作失败，天数小于或等于0。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            if (txtShopSpace.Text.Trim()!="")
            {
                if(!ChangeHope.Common.ValidateHelper.IsNumber(txtShopSpace.Text.Trim()))
                {
                    this.ltlMsg.Text = "操作失败，请输入正确空间大小。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                if(int.Parse(txtShopSpace.Text.Trim())<=0)
                {
                    this.ltlMsg.Text = "操作失败，空间大小小于或等于0。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            if (txtProCount.Text.Trim()!="")
            {
                if (!ChangeHope.Common.ValidateHelper.IsNumber(txtProCount.Text.Trim()))
                {
                    this.ltlMsg.Text = "操作失败，数量请输入数字。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                if(int.Parse(txtProCount.Text.Trim())<=0)
                {
                    this.ltlMsg.Text = "操作失败，数量小于或等于0。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }

            }
            if (txtSort.Text.Trim()!="")
            {
                if(!ChangeHope.Common.ValidateHelper.IsNumber(txtSort.Text.Trim()))
                {
                    this.ltlMsg.Text = "操作失败，排序请输入数字。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            model.Price = Convert.ToDecimal(this.txtPrice.Text);
            model.ExpendDays = Convert.ToInt32(this.txtExpendDays.Text);
            model.ProCount = Convert.ToInt32(this.txtProCount.Text);
            model.Content = this.txtContent.Text;
            model.Attribute = this.txtAttribute.Text;
            model.ShopSpace = Convert.ToInt32(this.txtShopSpace.Text);
            model.Sort = Convert.ToInt32(this.txtSort.Text);
            if (ViewState["ID"] != null)
            {
                model.ID = Convert.ToInt32(ViewState["ID"].ToString());
                bll.Amend(model);
                this.ltlMsg.Text = "操作成功，已修改该信息";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
                return;
            }
            else
            {
                bll.Add(model);
                this.ltlMsg.Text = "操作成功，已添加该信息";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
            }
        }

        /// <summary>
        /// 显示编辑信息
        /// </summary>
        /// <param name="id"></param>
        protected void BandInfo(int id)
        {
            YXShop.BLL.Shop.ShopLevel bll = new YXShop.BLL.Shop.ShopLevel();
            YXShop.Model.Shop.ShopLevel model = bll.GetModelByID(id);
            this.txtName.Text = model.Name;
            this.txtPrice.Text = model.Price.ToString();
            this.txtExpendDays.Text = model.ExpendDays.ToString();
            this.txtProCount.Text = model.ProCount.ToString();
            this.txtContent.Text = model.Content;
            this.txtAttribute.Text = model.Attribute;
            this.txtShopSpace.Text = model.ShopSpace.ToString();
            this.txtSort.Text = model.Sort.ToString();
            ViewState["ID"] = model.ID.ToString();
           
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            Save();
        }
    }
}
